package com.wholeintech.entity;

import android.util.Log;

import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;


@Table(name = "KnxDevice")
public class KnxDevice extends EntityBase {
    public static final String TAG = "KnxDevice";

    @Column(name = "devId")
    private String devId;

    @Column(name = "swOpen")
    private long swOpen;

    @Column(name = "swClose")
    private long swClose;

    @Column(name = "swUp")
    private long swUp;

    @Column(name = "swDown")
    private long swDown;

    @Column(name = "swStop")
    private long swStop;

    @Column(name = "swFb")
    private long swFb;

    @Column(name = "brightness")
    private long brightness;

    @Column(name = "brightnessFb")
    private long brightnessFb;

    @Column(name = "tempe")
    private long tempe;

    @Column(name = "tempeFb")
    private long tempeFb;

    @Column(name = "windAuto")
    private long windAuto;
    private long windManual;

    @Column(name = "wind0")
    private long wind0;
    @Column(name = "wind1")
    private long wind1;
    @Column(name = "wind2")
    private long wind2;
    @Column(name = "wind3")
    private long wind3;
    @Column(name = "wind4")
    private long wind4;
    @Column(name = "wind5")
    private long wind5;
    @Column(name = "wind6")
    private long wind6;
    @Column(name = "wind7")
    private long wind7;
    @Column(name = "windFb")
    private long windFb;

    @Column(name = "mode0")
    private long mode0;
    @Column(name = "mode1")
    private long mode1;
    @Column(name = "mode2")
    private long mode2;
    @Column(name = "mode3")
    private long mode3;
    @Column(name = "mode4")
    private long mode4;
    @Column(name = "mode5")
    private long mode5;
    @Column(name = "mode6")
    private long mode6;
    @Column(name = "mode7")
    private long mode7;
    @Column(name = "modeFb")
    private long modeFb;

    public String getDevId() {
        return devId;
    }

    public void setDevId(String devId) {
        this.devId = devId;
    }

    public long getSwOpen() {
        return swOpen;
    }

    public void setSwOpen(long swOpen) {
        this.swOpen = swOpen;
    }

    public long getSwClose() {
        return swClose;
    }

    public void setSwClose(long swClose) {
        this.swClose = swClose;
    }

    public long getSwUp() {
        return swUp;
    }

    public void setSwUp(long swUp) {
        this.swUp = swUp;
    }

    public long getSwDown() {
        return swDown;
    }

    public void setSwDown(long swDown) {
        this.swDown = swDown;
    }

    public long getSwStop() {
        return swStop;
    }

    public void setSwStop(long swStop) {
        this.swStop = swStop;
    }

    public long getSwFb() {
        return swFb;
    }

    public void setSwFb(long swFb) {
        this.swFb = swFb;
    }

    public long getBrightness() {
        return brightness;
    }

    public void setBrightness(long brightness) {
        this.brightness = brightness;
    }

    public long getBrightnessFb() {
        return brightnessFb;
    }

    public void setBrightnessFb(long brightnessFb) {
        this.brightnessFb = brightnessFb;
    }

    public long getTempe() {
        return tempe;
    }

    public void setTempe(long tempe) {
        this.tempe = tempe;
    }

    public long getTempeFb() {
        return tempeFb;
    }

    public void setTempeFb(long tempeFb) {
        this.tempeFb = tempeFb;
    }

    public long getWindAuto() {
        return windAuto;
    }

    public void setWindAuto(long windAuto) {
        this.windAuto = windAuto;
    }

    public long getWindManual() {
        return windManual;
    }

    public void setWindManual(long windManual) {
        this.windManual = windManual;
    }

    public long getWind0() {
        return wind0;
    }

    public void setWind0(long wind0) {
        this.wind0 = wind0;
    }

    public long getWind1() {
        return wind1;
    }

    public void setWind1(long wind1) {
        this.wind1 = wind1;
    }

    public long getWind2() {
        return wind2;
    }

    public void setWind2(long wind2) {
        this.wind2 = wind2;
    }

    public long getWind3() {
        return wind3;
    }

    public void setWind3(long wind3) {
        this.wind3 = wind3;
    }

    public long getWind4() {
        return wind4;
    }

    public void setWind4(long wind4) {
        this.wind4 = wind4;
    }

    public long getWind5() {
        return wind5;
    }

    public void setWind5(long wind5) {
        this.wind5 = wind5;
    }

    public long getWind6() {
        return wind6;
    }

    public void setWind6(long wind6) {
        this.wind6 = wind6;
    }

    public long getWind7() {
        return wind7;
    }

    public void setWind7(long wind7) {
        this.wind7 = wind7;
    }

    public long getWindFb() {
        return windFb;
    }

    public void setWindFb(long windFb) {
        this.windFb = windFb;
    }

    public long getMode0() {
        return mode0;
    }

    public void setMode0(long mode0) {
        this.mode0 = mode0;
    }

    public long getMode1() {
        return mode1;
    }

    public void setMode1(long mode1) {
        this.mode1 = mode1;
    }

    public long getMode2() {
        return mode2;
    }

    public void setMode2(long mode2) {
        this.mode2 = mode2;
    }

    public long getMode3() {
        return mode3;
    }

    public void setMode3(long mode3) {
        this.mode3 = mode3;
    }

    public long getMode4() {
        return mode4;
    }

    public void setMode4(long mode4) {
        this.mode4 = mode4;
    }

    public long getMode5() {
        return mode5;
    }

    public void setMode5(long mode5) {
        this.mode5 = mode5;
    }

    public long getMode6() {
        return mode6;
    }

    public void setMode6(long mode6) {
        this.mode6 = mode6;
    }

    public long getMode7() {
        return mode7;
    }

    public void setMode7(long mode7) {
        this.mode7 = mode7;
    }

    public long getModeFb() {
        return modeFb;
    }

    public void setModeFb(long modeFb) {
        this.modeFb = modeFb;
    }

    //knx utils
    /*
     *  struct KnxCmd (64bit)
     *  {
     *  	uint8_t  mainGroup;
     *  	uint8_t  middleGroup;
     *  	uint8_t  Group;
     *  	uint8_t  bits;
     *  	uint32_t value;
     *  }
     *
     */
    static public int getMainGroup(long cmd) {
        cmd >>= 32 + 24;
        return (int) (cmd & 0xFF);
    }

    static public int getMiddleGroup(long cmd) {
        cmd >>= 32 + 16;
        return (int) (cmd & 0xFF);
    }

    static public int getGroup(long cmd) {
        cmd >>= 32 + 8;
        return (int) (cmd & 0xFF);
    }

    static public int getBits(long cmd) {
        cmd >>= 32 + 0;
        return (int) (cmd & 0xFF);
    }

    static public int getValue(long cmd) {
        return (int) (cmd & 0xFFFFFFFF);
    }

    static public boolean isCmdValid(long cmd) {
        return isCmdValid(cmd, "");
    }

    static public boolean isCmdValid(long cmd, String name) {
        int main, middle, group, bits, value;
        main = getMainGroup(cmd);
        middle = getMiddleGroup(cmd);
        group = getGroup(cmd);
        bits = getBits(cmd);
        value = getValue(cmd);

        if (main == 0 && middle == 0 && group == 0)
            return false;
        if (bits != 1 && bits != 4 && bits != 8 && bits != 16 && bits != 24 && bits != 32)
            return false;
        if (main < 0 || main >= 64)
            return false;
        if (middle < 0 || middle >= 64)
            return false;
        if (group < 0 || group >= 64)
            return false;
        if (value < 0 || value >= 65535)
            return false;

        Log.d(TAG, String.format("%s cmd: %d/%d/%d=%d@%dbits", name, main, middle, group, value, bits));
        return true;
    }

    static public long buildCmdLong(long mainGroup, long middleGroup, long group, long bits, long value) {
        long cmd = 0;
        Log.d(TAG, String.format("knx build cmd: %d/%d/%d=%d@%dbits", mainGroup, middleGroup, group, value, bits));

        cmd |= (mainGroup & 0xFF) << (32 + 24);
        cmd |= (middleGroup & 0xFF) << (32 + 16);
        cmd |= (group & 0xFF) << (32 + 8);
        cmd |= (bits & 0xFF) << (32 + 0);
        cmd |= (value & 0xFFFFFFFF);
        return cmd;
    }

    static public long parserPathCmd(String cmd) {
        int main, middle, group, bits, value;
        main = middle = group = bits = value = -1;

        if (cmd == null || cmd.length() < 4) //" / / = @ "
            return -1;
        try {
            String[] v;
            String[] s = cmd.split("=");
            if (s == null)
                return -1;
            if (s.length == 2) {
                v = s[0].split("/");
                if (v.length == 3) {
                    main = Integer.parseInt(v[0]);
                    middle = Integer.parseInt(v[1]);
                    group = Integer.parseInt(v[2]);
                } else {
                    main = 0xFF;
                    middle = 0xFF;
                    group = 0xFF;
                }

                v = s[1].split("@");
                if (v.length == 2) {
                    value = Integer.parseInt(v[0]);
                    bits = Integer.parseInt(v[1]);
                } else {
                    value = 0xFFFFFFFF;
                    bits = 0xFF;
                }
            } else if (s.length == 1) {
                main = 0xFF;
                middle = 0xFF;
                group = 0xFF;
                value = 0xFFFFFFFF;
                bits = 0xFF;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buildCmdLong(main, middle, group, bits, value);
    }

    public String dump() {
        String s = "";
        Log.d(TAG, "DevId:" + devId);
        //FIXME: use log to print.
        isCmdValid(swOpen, "swOpen");
        isCmdValid(swClose, "swClose");
        isCmdValid(swUp, "swUp");
        isCmdValid(swDown, "swDown");
        isCmdValid(swStop, "swStop");
        isCmdValid(swFb, "swFb");
        isCmdValid(brightness, "brightness");
        isCmdValid(brightnessFb, "brightnessFb");
        isCmdValid(tempe, "tempe");
        isCmdValid(tempeFb, "tempeFb");

        isCmdValid(windAuto, "windAuto");
        isCmdValid(windManual, "windManual");
        isCmdValid(wind0, "wind0");
        isCmdValid(wind1, "wind1");
        isCmdValid(wind2, "wind2");
        isCmdValid(wind3, "wind3");
        isCmdValid(wind4, "wind4");
        isCmdValid(wind5, "wind5");
        isCmdValid(wind6, "wind6");
        isCmdValid(wind7, "wind7");
        isCmdValid(windFb, "windFb");

        isCmdValid(mode0, "mode0");
        isCmdValid(mode1, "mode1");
        isCmdValid(mode2, "mode2");
        isCmdValid(mode3, "mode3");
        isCmdValid(mode4, "mode4");
        isCmdValid(mode5, "mode5");
        isCmdValid(mode6, "mode6");
        isCmdValid(mode7, "mode7");
        isCmdValid(modeFb, "modeFb");

        return devId;
    }
}

